package cn.virens.database.common;

import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

import tk.mybatis.mapper.entity.Example;

public class ExampleWrapper extends Example {

	public ExampleWrapper(Class<?> entityClass) {
		super(entityClass);
	}

	public static <T> ExampleWrapper of(Class<T> clazz) {
		return new ExampleWrapper(clazz);
	}

	public static <T> ExampleWrapper of(Class<T> clazz, Map<String, Object> param) {
		ExampleWrapper answer = new ExampleWrapper(clazz);
		answer.createCriteria(param);

		return answer;
	}

	public void or(Consumer<Criteria> consumer) {
		consumer.accept(or());
	}

	public void and(Consumer<Criteria> consumer) {
		consumer.accept(and());
	}

	public Criteria createCriteria(Map<String, Object> param) {
		if (param == null || param.isEmpty()) { return null; }

		Criteria criteria = super.or();

		for (Entry<String, Object> entry : param.entrySet()) {
			criteria.andEqualTo(entry.getKey(), entry.getValue());
		}

		return criteria;
	}
}